﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.Core.DataAdapter;
using Sealight.API.Framework;
using Sealight.Core.Domain;
using Sealight.Data;
using Sealight.API.Ioc;

namespace Sealight.CoreAPI
{
  public  class RoleService: IRoleService
    {
        #region IRoleService Members

        public role AddOrUpdate(Core.DataAdapter.role role)
        {
            if (role == null)
                throw new ArgumentNullException("RoleEntity Data is NULL.");

            if(string.IsNullOrEmpty(role.role_name))
                throw new ArgumentNullException("Role Name cannot be Null.");
            #region start_unit_of_work
            IUnitOfWork uow = null;
            if (!UnitOfWork.IsStarted)
                uow = UnitOfWork.Start();
            else uow = UnitOfWork.Current;
            #endregion
            bool commitTrans = false;
            if (!uow.IsInActiveTransaction)
            {
                //if not in active transaction
                //then begin transaction and commit it later
                uow.BeginTransaction(); //start transaction
                commitTrans = true;                
            }
            try
            {
                IRoleRepository repoRole = IocContainer.Container().Resolve<IRoleRepository>();

                RoleEntity data = repoRole.GetById(role.id);

                if(data==null)
                    data = new RoleEntity();

                data.RoleName = role.role_name;

                repoRole.SaveOrUpdate(data);

                 if (commitTrans)
                    uow.TransactionalFlush();

                 role = data.ToDataAdapter();
            }
            catch(Exception ex)
            {
                throw new Exception("Error saving role data. ErrMsg: " + ex.Message, ex);
            }

            return role;
        }
        
        public Core.DataAdapter.role GetById(Guid id)
        {
            RoleEntity data = null;

            try
            {
                if (id == Guid.Empty)
                    throw new ArgumentNullException("Role ID cannot be NULL.");

                #region start_unit_of_work
                IUnitOfWork uow = null;
                if (!UnitOfWork.IsStarted)
                    uow = UnitOfWork.Start();
                else uow = UnitOfWork.Current;
                #endregion

                IRoleRepository repo = IocContainer.Container().Resolve<IRoleRepository>();

                data = repo.GetById(id);
            }
            catch (Exception ex)
            {
                throw new Exception("Error get role data. ErrMsg: " + ex.Message, ex);
            }

            if (data != null)
                return data.ToDataAdapter();

            return null;
        }

        public IList<Core.DataAdapter.role> GetAll()
        {
            IList<RoleEntity> listData = null;
            try
            {
                #region start_unit_of_work
                IUnitOfWork uow = null;
                if (!UnitOfWork.IsStarted)
                    uow = UnitOfWork.Start();
                else uow = UnitOfWork.Current;
                #endregion
                IRoleRepository repo = IocContainer.Container().Resolve<IRoleRepository>();

                listData = repo.GetAll();
            }
            catch (Exception ex)
            {
                throw new Exception("Error get role data. ErrMsg: " + ex.Message, ex);
            }

            IList<role> dataAdapter = null;
            if (listData != null)
            {
                dataAdapter = new List<role>();

                foreach (RoleEntity entity in listData)
                {
                    dataAdapter.Add(entity.ToDataAdapter());
                }
            }

            return dataAdapter;
        }

        #endregion

        #region IRoleService Members


        public void Delete(role role)
        {
            try
            {
                RoleEntity data = null;

                if (data == null)
                    throw new ArgumentNullException("Role Data cannot be NULL.");

                #region start_unit_of_work
                IUnitOfWork uow = null;
                if (!UnitOfWork.IsStarted)
                    uow = UnitOfWork.Start();
                else uow = UnitOfWork.Current;
                #endregion

                IRoleRepository repo = IocContainer.Container().Resolve<IRoleRepository>();

                data = repo.GetById(role.id);

                if (data != null)
                    repo.Delete(data);
            }
            catch (Exception ex)
            {
                throw new Exception("Error get role data. ErrMsg: " + ex.Message, ex);
            }
        }

        #endregion
    }
}
